/*
*  server.c
*  
*
*  Created by Viktor Kurz & Stefan Wrobel on 08.12.09.
*  Copyright 2009. All rights reserved.
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>


int main (int argc, const char * argv[]) {
   int sock;
   int len;
   int i;

   struct sockaddr_in server_addr;  
   struct sockaddr_in source_addr; 
   unsigned int source_addr_len = sizeof(struct sockaddr);
   
   char buffer[255] = "";
   
   server_addr.sin_family = AF_INET;
   server_addr.sin_port   = htons(1234);
   server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
   
   if((sock =  socket(AF_INET , SOCK_DGRAM, 0)) == -1) {
      perror("Fehler bei socket!");
   }
   
   if(bind(sock, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in)) == -1) {
      perror("Fehler bei bind!");
   }
   
   do {
      if((len = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *) &source_addr, &source_addr_len)) == -1) {
         perror("Fehler bei recvfrom!");
      }
      
      for(i = 0; i < len; i++) {
         if(buffer[i] == '\n') buffer[i] = '\0';
         buffer[i] = toupper(buffer[i]);
      }
      
      printf("%s\n", buffer);
      
      if((len = sendto(sock, buffer, len, 0,(struct sockaddr*) &source_addr, sizeof(struct sockaddr_in))) == -1){
         perror("Fehler bei sendto!");
      }
      
   } while(strcmp(buffer, "EXIT\0") != 0);
   
      
   
   return 0;
}